package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.domain.Answer;

import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.api.domain.Question;
import cn.wolfcode.wolf2w.business.query.QuestionQuery;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;


import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 提问信息 Controller
 *
 * @author libai
 */
@RestController
@RequestMapping("destinationDoubt")
@Api(value = "问答模块操作的控制器接口", description = "destinationDoubt资源控制器")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;

    /**
     * 提问信息详情
     */
    @ApiOperation(value = "获取问答详情")
    @ApiImplicitParam(value = "问题id", name = "id", dataType = "Long", readOnly = true)
    @GetMapping("/detail/{id}")
    public R<Question> detail(@PathVariable Long id) {
        Question question = questionService.getDetail(id);
        return R.ok(question);
    }

    /**
     * 提问信息 列表
     */
    @ApiOperation(value = "问答分页")
    @GetMapping("/page")
    public R<IPage<Question>> query(QuestionQuery qo) {
        System.out.println(qo.getTab());
        IPage<Question> page = questionService.queryPage(qo);
        return R.ok(page);
    }


    //回复问题
    @ApiOperation(value = "回复问题")
    @PostMapping("/answer")
    public R<?> answer(@RequestBody Answer answer) {
        Long uid = SecurityContextHolder.getUserId();
        questionService.answer(answer, uid);
        return R.ok();
    }

    /**
     * 新增问题
     */
    @ApiOperation(value = "新增问题")
    @RequiresLogin
    @PostMapping("/create")
    public R<?> create(@RequestBody Question question) {
        Long uid = SecurityContextHolder.getUserId();
        questionService.create(question, uid);
        return R.ok();
    }

    /**
     * 点赞
     */
    @ApiOperation(value = "问答点赞")
    @ApiImplicitParam(value = "问题id", name = "questionId", dataType = "Long", readOnly = true)
    @PostMapping("/zan")
    public R<Map<String, Object>> zan(Long questionId) {
        Long uid = SecurityContextHolder.getUserId();
        Map<String, Object> map = questionService.zan(questionId, uid);
        return R.ok(map);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @ApiOperation(value = "问题列表远程调用")
    @GetMapping("/feign/list")
    public R<List<Question>> feignList() {
        return R.ok(questionService.list());
    }

    @ApiOperation(value = "获取问题远程调用")
    @ApiImplicitParam(value = "问题id", name = "id", dataType = "Long", readOnly = true)
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Question> feignGet(@PathVariable Long id) {
        return R.ok(questionService.getById(id));
    }
}
